package com.ev.apis.controller.asl;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.ev.asl.service.AssemblyProductionStatisticsService;
import com.ev.asl.vo.AssemblyProductionStatisticsVo;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.JSONUtils;
import com.ev.framework.utils.R;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 组装生产统计表分析
 * @author 姚家乐
 * @since 2021/5/18 11:30
 */
@Api(value = "/", tags = "组装生产统计表分析API")
@RestController
public class AssemblyProductionStatisticsApiController {

    @Autowired
    private AssemblyProductionStatisticsService service;

    @EvApiByToken(value = "/apis/asl/Aps/accounting", method = RequestMethod.POST, apiTitle = "组装生产统计表分析数据")
    @ApiOperation("组装型生产统计表分析数据")
    public R accounting(@ApiParam(value = "日期的起始时间") @RequestParam(value = "indicateDateStart", defaultValue = "", required = false) Date indicateDateStart,
                        @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
                        @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "10") int pagesize,
                        @ApiParam(value = "日期的结束时间") @RequestParam(value = "indicateDateEnd", defaultValue = "", required = false) Date indicateDateEnd) {
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        param.put("indicateDateStart", indicateDateStart);
        param.put("indicateDateEnd", indicateDateEnd);
        param.put("offset", (pageno - 1) * pagesize);
        param.put("limit", pagesize);
        param.put("over", 1);
        Map<String, Object> results = Maps.newHashMapWithExpectedSize(1);
        results.put("data", service.apsAccounting(param));
        return R.ok(results);
    }

    @EvApiByToken(value = "/apis/exportExcel/aslApsAccounting", method = RequestMethod.GET, apiTitle = "组装生产统计表分析数据(导出)")
    @ApiOperation("组装生产统计表分析数据(导出)")
    public void exportExcel(@ApiParam(value = "日期的起始时间") @RequestParam(value = "indicateDateStart", defaultValue = "", required = false) Date indicateDateStart,
                            @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
                            @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "10") int pagesize,
                            @ApiParam(value = "日期的结束时间") @RequestParam(value = "indicateDateEnd", defaultValue = "", required = false) Date indicateDateEnd,
                            HttpServletRequest request, HttpServletResponse response,
                            ModelMap modelMap) {
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        param.put("indicateDateStart", indicateDateStart);
        param.put("indicateDateEnd", indicateDateEnd);
        param.put("offset", (pageno - 1) * pagesize);
        param.put("limit", pagesize);
        AssemblyProductionStatisticsVo vo = service.apsAccounting(param);

        if (Objects.nonNull(vo)){
            ClassPathResource classPathResource = new ClassPathResource(
                    "poi/assembly_production_statistics.xlsx");
            Map<String, Object> map = Maps.newHashMap();
            map.put("timeProfiles", JSONUtils.format(vo.getTimeProfiles()));
            map.put("workingHoursTtl", vo.getWorkingHoursTtl());
            map.put("dischargingTimeTtl", vo.getDischargingTimeTtl());
            map.put("cleaningTimeTtl", vo.getCleaningTimeTtl());
            map.put("maintenanceTimeTtl", vo.getMaintenanceTimeTtl());
            map.put("meetingTimeTtl", vo.getMeetingTimeTtl());
            map.put("trainingTimeTtl", vo.getTrainingTimeTtl());
            map.put("ttlTimeTtl", vo.getTtlTimeTtl());

            map.put("totalMoldedQty", vo.getActualSituation().getTotalMoldedQty());
            map.put("qualifiedQty", vo.getActualSituation().getQualifiedQty());
            map.put("suiteInspectionQty", vo.getActualSituation().getSuiteInspectionQty());
            map.put("compInspectionQty", vo.getActualSituation().getCompInspectionQty());
            map.put("yield", vo.getActualSituation().getYield());

            map.put("badForms", JSONUtils.format(vo.getBadForms()));
            map.put("badFormQtyTotal", vo.getBadFormQtyTotal());

            TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
            modelMap.put(TemplateExcelConstants.FILE_NAME, "组装生产统计表");
            modelMap.put(TemplateExcelConstants.PARAMS, result);
            modelMap.put(TemplateExcelConstants.MAP_DATA, map);
            PoiBaseView.render(modelMap, request, response,
                    TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
        }
    }
}
